package com.chezhujie.www.controller;


import com.xy.core.page.Pagination;
import com.chezhujie.www.domain.*;
import com.chezhujie.www.enums.AwardType;
import com.chezhujie.www.service.*;
import com.xy.user.base.controller.BaseController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.*;




@Controller
@RequestMapping("awardstock")
public class CzjawardstockController extends BaseController {
	private static final Logger log = LoggerFactory.getLogger(CzjawardstockController.class);
	public CzjawardstockController() {
	}
 
 	/**
	 * czj_awardstock 奖品库新增页面
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("czjthingId.do")
	public void czjthingId(HttpServletRequest request,
			String discountShop,
			HttpServletResponse response, ModelMap model) throws Exception {
	      try {
              Map<String, Object> param = new HashMap<String, Object>();
              param.put("businessId", discountShop);
             List<Czjcommodity> czjcommodities = czjtop3Service.getCzjcommodityList(param);    	  
	         jsonResult(1, "", "获取对应商家的所有商品",czjcommodities, response);      
	         } catch (Exception e) {
	   			log.error("czjthingId.do:", e); 
	   			throw e;
	   		}
		
	} 
	
	
 	/**
	 * czj_awardstock 奖品库新增页面
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("czjawardstockAdd.do")
	public String add(HttpServletRequest request, ModelMap model) {
 		Map<String, Object> param2 = new HashMap<String, Object>();
        List<Czjfestivalact> czjfestivalacts = czjtop3Service.getCzjactListALL(param2);
 		Map<String, Object> param3 = new HashMap<String, Object>();
        List<Czjbusiness> czjbusinesses = czjtop3Service.getCzjbusinessListALL(param3);
        
        model.addAttribute("czjbusinesses", czjbusinesses);         
        model.addAttribute("czjfestivalacts", czjfestivalacts);
		model.addAttribute("czjawardstock", new Czjawardstock());
      setDict(model);
		return "awardstock/czjawardstockEdit";
	}

	/**
	 * czj_awardstock 奖品库保存
	 * 
	 * @param czjawardstock
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("czjawardstockstartSave.do")
	public String czjawardstockstartSave(Czjawardstock czjawardstock, 			
			HttpServletRequest request, ModelMap model) throws Exception{
		//要跳转的请求或页面
		String html =""; 
		try {
			if(czjawardstock.getAwardstockId()!=null&&!czjawardstock.getAwardstockId().equals("")){
				// 编辑
				czjawardstockService.updateCzjawardstock(czjawardstock);
				model.addAttribute("message",2);
				//tlogService.insertTloger(LoggerHelper.getTloger(request, LoggerHelper.OPERATING, "编辑czj_awardstock 奖品库", "awardstockId="+czjawardstock.getAwardstockId()));
			} else {
				// 新增
				czjawardstock.setCreatetime(new Date());
				czjawardstock.setDelFlag(0);
				czjawardstockService.insertCzjawardstock(czjawardstock);
				model.addAttribute("message",2);
				//tlogService.insertTloger(LoggerHelper.getTloger(request, LoggerHelper.OPERATING, "新增czj_awardstock 奖品库", "awardstockId="+czjawardstock.getAwardstockId()));
			}
   		} catch (Exception e) {
   			log.error("czjawardstock save error:", e); 
            throw e;
   		}
		if(czjawardstock.getAwardType()==1){
			html ="actEdit.do";
		}
		if(czjawardstock.getAwardType()==2){
			html ="youhuijuan.do";
		}
		if(czjawardstock.getAwardType()==3){
			html ="shiwu.do";
		}
		if(czjawardstock.getAwardType()==4){
			html ="hongbao.do";
		}
		if(czjawardstock.getAwardType()==5){
			html ="actnum.do";
		}
		if(czjawardstock.getAwardType()==6){
			html ="xiexie.do";
		}	      
		return "redirect:"+html+"?awardstockId="+czjawardstock.getAwardstockId();
	}
	
	
	/**
	 * czj_awardstock 奖品库保存
	 * 
	 * @param czjawardstock
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("czjawardstockDeail.do")
	public String czjawardstockDeail(String awardstockId, Czjawardstock czjawardstock,			
			HttpServletRequest request, ModelMap model) throws Exception{
		//要跳转的请求或页面
		String html =""; 
		try {
	   		Map<String, Object> param = new HashMap<String, Object>();
	   		param.put("awardstockId", awardstockId);
	         czjawardstock = czjawardstockService.getCzjawardstock(param);
	 		if(czjawardstock.getAwardType()==1){
				html ="actEditDetail.do";
			}
			if(czjawardstock.getAwardType()==2){
				html ="youhuijuanDetail.do";
			}
			if(czjawardstock.getAwardType()==3){
				html ="shiwuDetail.do";
			}
			if(czjawardstock.getAwardType()==4){
				html ="hongbaoDetail.do";
			}
			if(czjawardstock.getAwardType()==5){
				html ="actnumDetail.do";
			}
			if(czjawardstock.getAwardType()==6){
				html ="xiexieDetail.do";
			}		                  
   		} catch (Exception e) {
   			log.error("czjawardstock save error:", e); 
            throw e;
   		}      
		return "redirect:"+html+"?awardstockId="+czjawardstock.getAwardstockId();
	}	
		
	/**
	 * czj_awardstock 奖品库详情
	 * 
	 * @param awardstockId
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("actEditDetail.do")
	public String actEditDetail(String awardstockId, HttpServletRequest request, ModelMap model) throws Exception{
      try {
          String message = request.getParameter("message");                  
          request.setAttribute("message", message);
   		Map<String, Object> param = new HashMap<String, Object>();
   		param.put("awardstockId", awardstockId);
         Czjawardstock czjawardstock = czjawardstockService.getCzjawardstock(param);
         
 		Map<String, Object> param2 = new HashMap<String, Object>();
        List<Czjfestivalact> czjfestivalacts = czjtop3Service.getCzjactListALL(param2);
        
        model.addAttribute("czjfestivalacts", czjfestivalacts);
   		model.addAttribute("czjawardstock", czjawardstock);
         model.addAttribute("actionEdit", request.getParameter("actionEdit"));
         setDict(model);
         } catch (Exception e) {
   			log.error("czjawardstock edit error:", e); 
            throw e;
   		}
		return "awardstock/detailType/actEdit";
	}
	
	/**
	 * czj_awardstock 奖品库详情
	 * 
	 * @param awardstockId
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("youhuijuanDetail.do")
	public String youhuijuanDetail(String awardstockId, HttpServletRequest request, ModelMap model) throws Exception{
      try {
          String message = request.getParameter("message");                  
          request.setAttribute("message", message);
   		Map<String, Object> param = new HashMap<String, Object>();
   		param.put("awardstockId", awardstockId);
         Czjawardstock czjawardstock = czjawardstockService.getCzjawardstock(param);
         

 
 		Map<String, Object> param3 = new HashMap<String, Object>();
        List<Czjbusiness> czjbusinesses = czjtop3Service.getCzjbusinessListALL(param3);
        
        
        if(!"-1".equals(czjawardstock.getDiscountShop())){      	       
 		Map<String, Object> param4 = new HashMap<String, Object>();
 		param4.put("businessId", czjawardstock.getDiscountShop());
        List<Czjcommodity> czjcommodities = czjtop3Service.getCzjcommodityList(param4); 
        model.addAttribute("czjcommodities", czjcommodities); 
        }       
        model.addAttribute("czjbusinesses", czjbusinesses);               
   		model.addAttribute("czjawardstock", czjawardstock);
         model.addAttribute("actionEdit", request.getParameter("actionEdit"));
         setDict(model);
         } catch (Exception e) {
   			log.error("czjawardstock edit error:", e); 
            throw e;
   		}
		return "awardstock/detailType/youhuijuanEdit";
	}
	/**
	 * czj_awardstock 奖品库详情
	 * 
	 * @param awardstockId
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("shiwuDetail.do")
	public String shiwuDetail(String awardstockId, HttpServletRequest request, ModelMap model) throws Exception{
      try {
          String message = request.getParameter("message");                  
          request.setAttribute("message", message);
   		Map<String, Object> param = new HashMap<String, Object>();
   		param.put("awardstockId", awardstockId);
         Czjawardstock czjawardstock = czjawardstockService.getCzjawardstock(param);
          
 		Map<String, Object> param3 = new HashMap<String, Object>();
        List<Czjbusiness> czjbusinesses = czjtop3Service.getCzjbusinessListALL(param3);
        
        
        if(!"-1".equals(czjawardstock.getDiscountShop())){      	       
 		Map<String, Object> param4 = new HashMap<String, Object>();
 		param4.put("businessId", czjawardstock.getDiscountShop());
        List<Czjcommodity> czjcommodities = czjtop3Service.getCzjcommodityList(param4); 
        model.addAttribute("czjcommodities", czjcommodities); 
        }       
        model.addAttribute("czjbusinesses", czjbusinesses);               
   		model.addAttribute("czjawardstock", czjawardstock);
         model.addAttribute("actionEdit", request.getParameter("actionEdit"));
         setDict(model);
         } catch (Exception e) {
   			log.error("czjawardstock edit error:", e); 
            throw e;
   		}
		return "awardstock/detailType/shiwuEdit";
	}
	
	/**
	 * czj_awardstock 奖品库详情
	 * 
	 * @param awardstockId
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("hongbaoDetail.do")
	public String hongbaoDetail(String awardstockId, HttpServletRequest request, ModelMap model) throws Exception{
      try {
          String message = request.getParameter("message");                  
          request.setAttribute("message", message);
   		Map<String, Object> param = new HashMap<String, Object>();
   		param.put("awardstockId", awardstockId);
         Czjawardstock czjawardstock = czjawardstockService.getCzjawardstock(param);
         
           
        if(!"-1".equals(czjawardstock.getDiscountShop())){      	       
 		Map<String, Object> param4 = new HashMap<String, Object>();
 		param4.put("businessId", czjawardstock.getDiscountShop());
        List<Czjcommodity> czjcommodities = czjtop3Service.getCzjcommodityList(param4); 
        model.addAttribute("czjcommodities", czjcommodities); 
        }       
   		model.addAttribute("czjawardstock", czjawardstock);
         model.addAttribute("actionEdit", request.getParameter("actionEdit"));
         setDict(model);
         } catch (Exception e) {
   			log.error("czjawardstock edit error:", e); 
            throw e;
   		}
		return "awardstock/detailType/hongbaoEdit";
	}
	
	/**
	 * czj_awardstock 奖品库详情
	 * 
	 * @param awardstockId
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("actnumDetail.do")
	public String actnumDetail(String awardstockId, HttpServletRequest request, ModelMap model) throws Exception{
      try {
          String message = request.getParameter("message");                  
          request.setAttribute("message", message);
   		Map<String, Object> param = new HashMap<String, Object>();
   		param.put("awardstockId", awardstockId);
         Czjawardstock czjawardstock = czjawardstockService.getCzjawardstock(param);
         
 		Map<String, Object> param2 = new HashMap<String, Object>();
        List<Czjfestivalact> czjfestivalacts = czjtop3Service.getCzjactListALL(param2);
         
        if(!"-1".equals(czjawardstock.getDiscountShop())){      	       
 		Map<String, Object> param4 = new HashMap<String, Object>();
 		param4.put("businessId", czjawardstock.getDiscountShop());
        List<Czjcommodity> czjcommodities = czjtop3Service.getCzjcommodityList(param4); 
        model.addAttribute("czjcommodities", czjcommodities); 
        }       
           
        model.addAttribute("czjfestivalacts", czjfestivalacts);
   		model.addAttribute("czjawardstock", czjawardstock);
         model.addAttribute("actionEdit", request.getParameter("actionEdit"));
         setDict(model);
         } catch (Exception e) {
   			log.error("czjawardstock edit error:", e); 
            throw e;
   		}
		return "awardstock/detailType/actnumEdit";
	}
	
	
	/**
	 * czj_awardstock 奖品库详情
	 * 
	 * @param awardstockId
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("xiexieDetail.do")
	public String xiexieDetail(String awardstockId, HttpServletRequest request, ModelMap model) throws Exception{
      try {
          String message = request.getParameter("message");                  
          request.setAttribute("message", message);
   		Map<String, Object> param = new HashMap<String, Object>();
   		param.put("awardstockId", awardstockId);
         Czjawardstock czjawardstock = czjawardstockService.getCzjawardstock(param);                          
   		model.addAttribute("czjawardstock", czjawardstock);
         model.addAttribute("actionEdit", request.getParameter("actionEdit"));
         setDict(model);
         } catch (Exception e) {
   			log.error("czjawardstock edit error:", e); 
            throw e;
   		}
		return "awardstock/detailType/xiexieEdit";
	}	
	
	/**
	 * czj_awardstock 奖品库保存
	 * 
	 * @param czjawardstock
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("czjawardstockSave.do")
	public String save(Czjawardstock czjawardstock, 
			@RequestParam(value = "second", required = false) MultipartFile second,
			@RequestParam(value = "third", required = false)  MultipartFile thrid,
			String oldCount,
			HttpServletRequest request, ModelMap model) throws Exception{
		//路径
		String pathRoot = request.getSession().getServletContext()
				.getRealPath("");
		String path = "";
		// 第二张头像
		if (!second.isEmpty() && second.getSize()>0) {
			String uuid = UUID.randomUUID().toString().replaceAll("-", "");
			path = "/uploadvoteorder/" + uuid;
			File file = new File(pathRoot + "/uploadvoteorder");
			if (!file.exists()) {
				file.mkdir();
			}
			second.transferTo(new File(pathRoot + path+".jpg"));
			/*Thumbnails.of(c).scale(0.25f).outputFormat("jpg")
         .toFile(new File(pathRoot + path+"czj"));//中等图
         */		
//			Thumbnails.of(new FileInputStream(new File(pathRoot + path+"."+imageName))).scale(1f).outputFormat("jpg").
//			outputQuality(0.25f).toFile(new File(pathRoot + path));
//			 Picture.resizePNG(pathRoot + path+"."+imageName, pathRoot + path+".jpg", 750, 750, true);
//				ImageUtils.fromFile(new File(pathRoot + path+".jpg"))
//				.quality(0.6f)
//				.fixedGivenSize(true)
//				.keepRatio(true)
//				.bgcolor(Color.WHITE)	//白色背景
//				.toFile(new File(pathRoot+path+"czj.jpg"));			 
			//更新才设置图片路径				
			czjawardstock.setPrizePath(path+".jpg");
		}				
		
		// 第三张头像
		if (!thrid.isEmpty() && thrid.getSize()>0) {
			String uuid = UUID.randomUUID().toString().replaceAll("-", "");
			path = "/uploadvoteorder/" + uuid + "." + "jpg";
			File file = new File(pathRoot + "/uploadvoteorder");
			if (!file.exists()) {
				file.mkdir();
			}
			thrid.transferTo(new File(pathRoot + path));					
			//更新才删
			String jpgPic = czjawardstock.getBackgroundPath();					
			if(jpgPic != null && jpgPic.length()>5){
				jpgPic = jpgPic.substring(1, jpgPic.length());
			}
			deleteFile(pathRoot + jpgPic);
			//更新才设置图片路径				
			czjawardstock.setBackgroundPath(path);
		}  
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");		
			String DateEnd = request.getParameter("validDate");
			String  endTimeStr = "23:59:59";
			DateEnd = DateEnd +" "+ endTimeStr;				
			Date endtime = sdf.parse(DateEnd);		
			czjawardstock.setValidDate(endtime);
			
			if(czjawardstock.getAwardstockId()!=null&&!czjawardstock.getAwardstockId().equals("")){
				// 编辑
				
				Map<String, Object> param2 = new HashMap<String, Object>();
				param2.put("awardstockId", czjawardstock.getAwardstockId());				
				Czjawardstock oldeCzjawardstock = czjawardstockService.getCzjawardstock(param2);
				if(oldeCzjawardstock !=null){					
					//新数据
				   int shengyunum = czjawardstock.getCountnum();
				   //旧数据
				   if(oldCount==null || "".equals(oldCount)){
					   oldCount = "0";
				   }
				   if(oldeCzjawardstock.getShengyunum() == null){
					   oldeCzjawardstock.setShengyunum(0);
				   }
				   //旧的剩余数据
				   int oldshengyunum = oldeCzjawardstock.getShengyunum();
				   //相差数据
				   int betweennum = shengyunum - Integer.valueOf(oldCount);	
				   //加上来的数据
				   int updateShengyunum =  oldshengyunum + betweennum;
				   czjawardstock.setShengyunum(updateShengyunum);
				}									
				czjawardstockService.updateCzjawardstock(czjawardstock);
				model.addAttribute("message",2);
				//tlogService.insertTloger(LoggerHelper.getTloger(request, LoggerHelper.OPERATING, "编辑czj_awardstock 奖品库", "awardstockId="+czjawardstock.getAwardstockId()));
			} else {
				// 新增
				czjawardstock.setCreatetime(new Date());
				czjawardstock.setShengyunum(czjawardstock.getCountnum());
				czjawardstock.setChouqunum(0);
				czjawardstockService.insertCzjawardstock(czjawardstock);
				model.addAttribute("message",2);
				//tlogService.insertTloger(LoggerHelper.getTloger(request, LoggerHelper.OPERATING, "新增czj_awardstock 奖品库", "awardstockId="+czjawardstock.getAwardstockId()));
			}
   		} catch (Exception e) {
   			log.error("czjawardstock save error:", e); 
            throw e;
   		}
	   		
		return "redirect:czjawardstockList.do";
	}

	/**
	 * czj_awardstock 奖品库删除
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping("czjawardstockDelete.do")
	public String delete(@RequestParam("ids") List<String> awardstockIdList) throws Exception{
		try {
         if (awardstockIdList != null) {
   			czjawardstockService.deleteCzjawardstock(awardstockIdList);
   		}
         } catch (Exception e) {
   			log.error("czjawardstock delete error:", e); 
            throw e;
   		}
		return "redirect:czjawardstockList.do";
	}
	 
	/**
	 * czj_awardstock 奖品库编辑页面
	 * 
	 * @param awardstockId
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("czjawardstockEdit.do")
	public String edit(String awardstockId, HttpServletRequest request, ModelMap model) throws Exception{
      try {
          String message = request.getParameter("message");                  
          request.setAttribute("message", message);
   		Map<String, Object> param = new HashMap<String, Object>();
   		param.put("awardstockId", awardstockId);
         Czjawardstock czjawardstock = czjawardstockService.getCzjawardstock(param);
         
 		Map<String, Object> param2 = new HashMap<String, Object>();
        List<Czjfestivalact> czjfestivalacts = czjtop3Service.getCzjactListALL(param2);
 
 		Map<String, Object> param3 = new HashMap<String, Object>();
        List<Czjbusiness> czjbusinesses = czjtop3Service.getCzjbusinessListALL(param3);
        
        
        if(!"-1".equals(czjawardstock.getDiscountShop())){      	       
 		Map<String, Object> param4 = new HashMap<String, Object>();
 		param4.put("businessId", czjawardstock.getDiscountShop());
        List<Czjcommodity> czjcommodities = czjtop3Service.getCzjcommodityList(param4); 
        model.addAttribute("czjcommodities", czjcommodities); 
        }       
        model.addAttribute("czjbusinesses", czjbusinesses);               
        model.addAttribute("czjfestivalacts", czjfestivalacts);
   		model.addAttribute("czjawardstock", czjawardstock);
         model.addAttribute("actionEdit", request.getParameter("actionEdit"));
         setDict(model);
         } catch (Exception e) {
   			log.error("czjawardstock edit error:", e); 
            throw e;
   		}
		return "awardstock/czjawardstockEdit";
	}				
//编辑页面	
	/**
	 * czj_awardstock 奖品库编辑页面
	 * 
	 * @param awardstockId
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("actEdit.do")
	public String actEdit(String awardstockId, HttpServletRequest request, ModelMap model) throws Exception{
      try {
          String message = request.getParameter("message");                  
          request.setAttribute("message", message);
   		Map<String, Object> param = new HashMap<String, Object>();
   		param.put("awardstockId", awardstockId);
         Czjawardstock czjawardstock = czjawardstockService.getCzjawardstock(param);
         
 		Map<String, Object> param2 = new HashMap<String, Object>();
        List<Czjfestivalact> czjfestivalacts = czjtop3Service.getCzjactListALL(param2);
        
        model.addAttribute("czjfestivalacts", czjfestivalacts);
   		model.addAttribute("czjawardstock", czjawardstock);
         model.addAttribute("actionEdit", request.getParameter("actionEdit"));
         setDict(model);
         } catch (Exception e) {
   			log.error("czjawardstock edit error:", e); 
            throw e;
   		}
		return "awardstock/type/actEdit";
	}
	
	/**
	 * czj_awardstock 奖品库编辑页面
	 * 
	 * @param awardstockId
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("youhuijuan.do")
	public String youhuijuan(String awardstockId, HttpServletRequest request, ModelMap model) throws Exception{
      try {
          String message = request.getParameter("message");                  
          request.setAttribute("message", message);
   		Map<String, Object> param = new HashMap<String, Object>();
   		param.put("awardstockId", awardstockId);
         Czjawardstock czjawardstock = czjawardstockService.getCzjawardstock(param);
         

 
 		Map<String, Object> param3 = new HashMap<String, Object>();
        List<Czjbusiness> czjbusinesses = czjtop3Service.getCzjbusinessListALL(param3);
        
        
        if(!"-1".equals(czjawardstock.getDiscountShop())){      	       
 		Map<String, Object> param4 = new HashMap<String, Object>();
 		param4.put("businessId", czjawardstock.getDiscountShop());
        List<Czjcommodity> czjcommodities = czjtop3Service.getCzjcommodityList(param4); 
        model.addAttribute("czjcommodities", czjcommodities); 
        }       
        model.addAttribute("czjbusinesses", czjbusinesses);               
   		model.addAttribute("czjawardstock", czjawardstock);
         model.addAttribute("actionEdit", request.getParameter("actionEdit"));
         setDict(model);
         } catch (Exception e) {
   			log.error("czjawardstock edit error:", e); 
            throw e;
   		}
		return "awardstock/type/youhuijuanEdit";
	}
	/**
	 * czj_awardstock 奖品库编辑页面
	 * 
	 * @param awardstockId
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("shiwu.do")
	public String shiwu(String awardstockId, HttpServletRequest request, ModelMap model) throws Exception{
      try {
          String message = request.getParameter("message");                  
          request.setAttribute("message", message);
   		Map<String, Object> param = new HashMap<String, Object>();
   		param.put("awardstockId", awardstockId);
         Czjawardstock czjawardstock = czjawardstockService.getCzjawardstock(param);
          
 		Map<String, Object> param3 = new HashMap<String, Object>();
        List<Czjbusiness> czjbusinesses = czjtop3Service.getCzjbusinessListALL(param3);
        
        
        if(!"-1".equals(czjawardstock.getDiscountShop())){      	       
 		Map<String, Object> param4 = new HashMap<String, Object>();
 		param4.put("businessId", czjawardstock.getDiscountShop());
        List<Czjcommodity> czjcommodities = czjtop3Service.getCzjcommodityList(param4); 
        model.addAttribute("czjcommodities", czjcommodities); 
        }       
        model.addAttribute("czjbusinesses", czjbusinesses);               
   		model.addAttribute("czjawardstock", czjawardstock);
         model.addAttribute("actionEdit", request.getParameter("actionEdit"));
         setDict(model);
         } catch (Exception e) {
   			log.error("czjawardstock edit error:", e); 
            throw e;
   		}
		return "awardstock/type/shiwuEdit";
	}
	
	/**
	 * czj_awardstock 奖品库编辑页面
	 * 
	 * @param awardstockId
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("hongbao.do")
	public String hongbao(String awardstockId, HttpServletRequest request, ModelMap model) throws Exception{
      try {
          String message = request.getParameter("message");                  
          request.setAttribute("message", message);
   		Map<String, Object> param = new HashMap<String, Object>();
   		param.put("awardstockId", awardstockId);
         Czjawardstock czjawardstock = czjawardstockService.getCzjawardstock(param);
         
           
        if(!"-1".equals(czjawardstock.getDiscountShop())){      	       
 		Map<String, Object> param4 = new HashMap<String, Object>();
 		param4.put("businessId", czjawardstock.getDiscountShop());
        List<Czjcommodity> czjcommodities = czjtop3Service.getCzjcommodityList(param4); 
        model.addAttribute("czjcommodities", czjcommodities); 
        }       
   		model.addAttribute("czjawardstock", czjawardstock);
         model.addAttribute("actionEdit", request.getParameter("actionEdit"));
         setDict(model);
         } catch (Exception e) {
   			log.error("czjawardstock edit error:", e); 
            throw e;
   		}
		return "awardstock/type/hongbaoEdit";
	}
	
	/**
	 * czj_awardstock 奖品库编辑页面
	 * 
	 * @param awardstockId
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("actnum.do")
	public String actnum(String awardstockId, HttpServletRequest request, ModelMap model) throws Exception{
      try {
          String message = request.getParameter("message");                  
          request.setAttribute("message", message);
   		Map<String, Object> param = new HashMap<String, Object>();
   		param.put("awardstockId", awardstockId);
         Czjawardstock czjawardstock = czjawardstockService.getCzjawardstock(param);
         
 		Map<String, Object> param2 = new HashMap<String, Object>();
        List<Czjfestivalact> czjfestivalacts = czjtop3Service.getCzjactListALL(param2);
         
        if(!"-1".equals(czjawardstock.getDiscountShop())){      	       
 		Map<String, Object> param4 = new HashMap<String, Object>();
 		param4.put("businessId", czjawardstock.getDiscountShop());
        List<Czjcommodity> czjcommodities = czjtop3Service.getCzjcommodityList(param4); 
        model.addAttribute("czjcommodities", czjcommodities); 
        }       
           
        model.addAttribute("czjfestivalacts", czjfestivalacts);
   		model.addAttribute("czjawardstock", czjawardstock);
         model.addAttribute("actionEdit", request.getParameter("actionEdit"));
         setDict(model);
         } catch (Exception e) {
   			log.error("czjawardstock edit error:", e); 
            throw e;
   		}
		return "awardstock/type/actnumEdit";
	}
	
	
	/**
	 * czj_awardstock 奖品库编辑页面
	 * 
	 * @param awardstockId
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("xiexie.do")
	public String xiexie(String awardstockId, HttpServletRequest request, ModelMap model) throws Exception{
      try {
          String message = request.getParameter("message");                  
          request.setAttribute("message", message);
   		Map<String, Object> param = new HashMap<String, Object>();
   		param.put("awardstockId", awardstockId);
         Czjawardstock czjawardstock = czjawardstockService.getCzjawardstock(param);                          
   		model.addAttribute("czjawardstock", czjawardstock);
         model.addAttribute("actionEdit", request.getParameter("actionEdit"));
         setDict(model);
         } catch (Exception e) {
   			log.error("czjawardstock edit error:", e); 
            throw e;
   		}
		return "awardstock/type/xiexieEdit";
	}
	
	
			 
	/**
	 * czj_awardstock 奖品库查询（分页）
	 * 
	 * @param czjawardstock
	 * @param pageNo
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("czjawardstockList.do")
	public String listPage(Czjawardstock czjawardstock, Integer pageNo, String order,
			String sort,HttpServletRequest request, ModelMap model) throws Exception{
      try {   
   		if (pageNo == null)
   			pageNo = 1;
   		czjawardstock.setDelFlag(0);
         Pagination pagination = czjawardstockService.getCzjawardstockList(toMap(czjawardstock), 20, pageNo,
   				order, sort); 
         request.setAttribute("thingName", czjawardstock.getThingName());
         request.setAttribute("awardType", czjawardstock.getAwardType());
   		model.addAttribute("pagination", pagination);
         setDict(model);
         } catch (Exception e) {
			log.error("czjawardstock listPage error:", e); 
         throw e;
		   }
		return "awardstock/czjawardstockList";
	}
	 
	/**
	 * czj_awardstock 奖品库查询
	 * 
	 * @param czjawardstock
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("czjawardstockList1.do")
	public String list(Czjawardstock czjawardstock, HttpServletRequest request, ModelMap model) throws Exception{
      try {
   		Map<String,Object> map = czjawardstockService.getCzjawardstockList(toMap(czjawardstock));
   		model.addAttribute("list", map.get("rows"));
   		model.addAttribute("total", map.get("total"));
         setDict(model);
         } catch (Exception e) {
   			log.error("czjawardstock list error:", e); 
            throw e;
   		}
		return "awardstock/czjawardstockList";
	}
	 

 	/***
	 * 将页面输入信息转成map
	 * 
	 * @param czjawardstock
	 * @return
	 */	private Map<String,Object> toMap(Czjawardstock czjawardstock){
		Map<String,Object> map=new HashMap<String, Object>();
      		if(czjawardstock.getAwardstockId()!=null&&!czjawardstock.getAwardstockId().equals("")){
			map.put("awardstockId", czjawardstock.getAwardstockId());
		}
		if(czjawardstock.getAwardType()!=null&&!czjawardstock.getAwardType().equals("")){
			map.put("awardType", czjawardstock.getAwardType());
		}
		if(czjawardstock.getCountnum()!=null&&!czjawardstock.getCountnum().equals("")){
			map.put("countnum", czjawardstock.getCountnum());
		}
		if(czjawardstock.getThingName()!=null&&!czjawardstock.getThingName().equals("")){
			map.put("thingName", czjawardstock.getThingName());
		}
		if(czjawardstock.getContent()!=null&&!czjawardstock.getContent().equals("")){
			map.put("content", czjawardstock.getContent());
		}
		if(czjawardstock.getPrice()!=null&&!czjawardstock.getPrice().equals("")){
			map.put("price", czjawardstock.getPrice());
		}
		if(czjawardstock.getValidDate()!=null&&!czjawardstock.getValidDate().equals("")){
			map.put("validDate", czjawardstock.getValidDate());
		}
		if(czjawardstock.getPrizePath()!=null&&!czjawardstock.getPrizePath().equals("")){
			map.put("prizePath", czjawardstock.getPrizePath());
		}
		if(czjawardstock.getBackgroundPath()!=null&&!czjawardstock.getBackgroundPath().equals("")){
			map.put("backgroundPath", czjawardstock.getBackgroundPath());
		}
		if(czjawardstock.getCreatetime()!=null&&!czjawardstock.getCreatetime().equals("")){
			map.put("createtime", czjawardstock.getCreatetime());
		}
		if(czjawardstock.getDiscountPrice()!=null&&!czjawardstock.getDiscountPrice().equals("")){
			map.put("discountPrice", czjawardstock.getDiscountPrice());
		}
		if(czjawardstock.getDiscountType()!=null&&!czjawardstock.getDiscountType().equals("")){
			map.put("discountType", czjawardstock.getDiscountType());
		}
		if(czjawardstock.getDiscountShop()!=null&&!czjawardstock.getDiscountShop().equals("")){
			map.put("discountShop", czjawardstock.getDiscountShop());
		}
		if(czjawardstock.getDiscountUrl()!=null&&!czjawardstock.getDiscountUrl().equals("")){
			map.put("discountUrl", czjawardstock.getDiscountUrl());
		}
		if(czjawardstock.getExt1()!=null&&!czjawardstock.getExt1().equals("")){
			map.put("ext1", czjawardstock.getExt1());
		}
		if(czjawardstock.getExt2()!=null&&!czjawardstock.getExt2().equals("")){
			map.put("ext2", czjawardstock.getExt2());
		}
		if(czjawardstock.getExt3()!=null&&!czjawardstock.getExt3().equals("")){
			map.put("ext3", czjawardstock.getExt3());
		}
		if(czjawardstock.getExt4()!=null&&!czjawardstock.getExt4().equals("")){
			map.put("ext4", czjawardstock.getExt4());
		}
		if(czjawardstock.getExt5()!=null&&!czjawardstock.getExt5().equals("")){
			map.put("ext5", czjawardstock.getExt5());
		}
		if(czjawardstock.getExt6()!=null&&!czjawardstock.getExt6().equals("")){
			map.put("ext6", czjawardstock.getExt6());
		}
		if(czjawardstock.getExt7()!=null&&!czjawardstock.getExt7().equals("")){
			map.put("ext7", czjawardstock.getExt7());
		} 
		if(czjawardstock.getDelFlag()!=null&&!czjawardstock.getDelFlag().equals("")){
			map.put("delFlag", czjawardstock.getDelFlag());
		}   		
		return map;
	}
   
	/**
	 * 设置页面所需的数据字典
	 * @param model
	 */
	private void setDict(ModelMap model){
		
	}

	//删除更新的图片
	 public void deleteFile(String jgpName){	 
		 File f = new File(jgpName); // 输入要删除的文件位置
		 if(f.exists()){
			 f.delete();
		 }
		
	 }

	/**
	 * 奖品中奖明细
	 * @param id
	 * @param req
	 * @param resp
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("allocationDetail.do")
	public String allocationDetails(Integer pageSize,Integer pageNo,String id,HttpServletRequest req, HttpServletResponse resp, ModelMap model) throws Exception{

		List<Tlmap> tlmapEntityList = new ArrayList<Tlmap>();

		List<Tlmap> tlmapList = new ArrayList<Tlmap>();

		Czjawardstock awardstock = czjawardstockService.queryById(id);
		if(awardstock!=null&&awardstock.getAwardType()==AwardType.PLACES.getIndex()){//活动优惠名额
			//活动优惠名额 ：活动名称 、使用用户、状态、有效时间、创建时间
			Map<String,Object> data = czjawardstockService.getPlaces(id);
			model.addAttribute("entity", data);
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("awardId", id);
			Pagination pagination = czjawarduseService.getAwardList(map,pageSize,pageNo,"","");
			model.addAttribute("pagination", pagination);
			return "awardstock/placesDetail";

		}else if(awardstock!=null&&awardstock.getAwardType()==AwardType.ACTIVITYCOUPONS.getIndex()){
			//1活动优惠券/代金券 ：
			//活动名称、优惠金额、
			//使用用户、使用状态、有效时间、中奖时间
			Map<String,Object> data = czjawardstockService.getActCoupons(id);
			model.addAttribute("entity", data);
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("awardId", id);
			 Pagination pagination = czjawarduseService.getAwardList(map,pageSize,pageNo,"","");
			model.addAttribute("pagination", pagination);
			return "awardstock/activityDetail";

		}else if(awardstock!=null&&awardstock.getAwardType()==AwardType.COMMODITYCOUPONS.getIndex()){
			//2商品优惠券/代金券 ： 商品名称
			Map<String,Object> data = czjawardstockService.getCommodityCoupons(id);
			model.addAttribute("entity", data);
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("awardId", id);
			Pagination pagination = czjawarduseService.getAwardList(map,pageSize,pageNo,"","");
			model.addAttribute("pagination", pagination);
			return "awardstock/commodityDetail";
		}else if(awardstock!=null&&awardstock.getAwardType()==AwardType.PHYSICAL.getIndex()){
			//3实物商品
			Map<String,Object> data = czjawardstockService.getGoods(id);
			model.addAttribute("entity", data);
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("awardId", id);
			Pagination pagination = czjawarduseService.getAwardList(map,pageSize,pageNo,"","");
			model.addAttribute("pagination", pagination);
			return "awardstock/goodsDetail";

		}else if(awardstock!=null&&awardstock.getAwardType()==AwardType.REDPACKET.getIndex()){
			//4红包
			Map<String,Object> data = czjawardstockService.getRedpacket(id);
			model.addAttribute("entity", data);
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("awardId", id);
			Pagination pagination = czjawarduseService.getAwardList(map,pageSize,pageNo,"","");
			model.addAttribute("pagination", pagination);
			return "awardstock/redpacketDetail";

		}




		model.addAttribute("tlmapEntityList",tlmapEntityList);

		return "awardstock/allocationDetail";
	}

	/**
	 * 分配详情
	 * @param pageSize
	 * @param pageNo
	 * @param id
	 * @param req
	 * @param resp
	 * @param model
	 * @return
	 */
	@RequestMapping("distributionDetail.do")
	public String distributionDetail(Integer pageSize,Integer pageNo,String id,HttpServletRequest req, HttpServletResponse resp, ModelMap model){
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("awardId", id);
		Pagination pagination = czjawardonService.getDistributionDetail(param,pageSize,pageNo,"","");
		model.addAttribute("pagination", pagination);
		return "awardstock/distributionDetail";
	}


	@Resource
	private CzjawardstockService czjawardstockService;

	@Resource
	private CzjrouletteService czjrouletteService;	
	
	@Resource
	private Czjtop3Service czjtop3Service;
	@Resource
	private CzjawarduseService czjawarduseService;
	@Resource
	private CzjawardonService czjawardonService;

}